package com.nikkerball.apps;

import com.nikkerball.apps.darkhorse.dao.BaseDao;
import com.nikkerball.apps.darkhorse.dev.TestDataLoader;
import com.nikkerball.apps.darkhorse.rules.RuleEngine;
import com.nikkerball.apps.darkhorse.web.base.SecurePage;
import com.nikkerball.apps.darkhorse.web.classroom.ClassroomListPanel;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * Homepage
 */
public class HomePage extends SecurePage {

    // ----- class fields -----

    private static final long serialVersionUID = 1L;

    // ----- instance fields -----

    @SuppressWarnings({"UnusedDeclaration"})
    @SpringBean
    private BaseDao baseDao;

    @SuppressWarnings({"UnusedDeclaration"})
    @SpringBean
    private RuleEngine ruleEngine;

    private ClassroomListPanel classroomListPanel;

    private Label editLinkLabel;

    // ----- constructors -----

    /**
     * Constructor that is invoked when page is invoked without a session.
     *
     * @param parameters Page parameters
     */
    @SuppressWarnings({"UnusedDeclaration"})
    public HomePage(final PageParameters parameters) {

        TestDataLoader testDataLoader = new TestDataLoader(this.baseDao);
        testDataLoader.loadData();

        this.classroomListPanel = new ClassroomListPanel("classroomListPanel");
        add(this.classroomListPanel);

        this.editLinkLabel = new Label("editLinkLabel", new Model<String>("Edit Classrooms"));

        AjaxLink editLink = new AjaxLink<String>("editLink") {
            public void onClick(AjaxRequestTarget ajaxRequestTarget) {
                classroomListPanel.toggleEditMode();
                ajaxRequestTarget.addComponent(HomePage.this.classroomListPanel);
                editLinkLabel
                        .setDefaultModelObject(classroomListPanel.isEditMode() ? "Finish Editing" : "Edit Classrooms");
                ajaxRequestTarget.addComponent(this);
            }
        };

        editLink.add(this.editLinkLabel);
        add(editLink);

    }
}
